home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Tools 2
/
Amiga Tools 2.iso
/
demoversionen
/
upsey
/
src
/
database.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-09
|
7KB
|
318 lines
#include <dos/dos.h>
#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/libraries.h>
#include <libraries/commodities.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <functions.h>
/*
* Application specific include files
*/
#include "ups.h"
#include "main.h"
#include "database.h"
#include "functions.h"
char *your_area = NULL;
char *zip_codes = NULL;
BOOL
GetRecordList (struct List *record_list, STRPTR database)
{
int retval = FALSE;
char buf[512];
LONG error = FALSE;
struct AnchorPath *anchorpath;
if (anchorpath = AllocMem (sizeof (struct AnchorPath) + 512, MEMF_CLEAR))
{
anchorpath->ap_Strlen = 512;
if (database)
{
strcpy (buf, database);
AddPart (buf, "#?.ups", 512);
if ((error = MatchFirst (buf, anchorpath)) == 0)
strcpy (buf, anchorpath->ap_Buf);
MatchEnd (anchorpath);
}
else
{
strcpy (buf, "#?.ups");
if ((error = MatchFirst (buf, anchorpath)) == 0)
strcpy (buf, anchorpath->ap_Buf);
MatchEnd (anchorpath);
if (error)
{
strcpy (buf, "s:");
AddPart (buf, "#?.ups", 512);
if ((error = MatchFirst (buf, anchorpath)) == 0)
strcpy (buf, anchorpath->ap_Buf);
MatchEnd (anchorpath);
}
}
FreeMem (anchorpath, sizeof (struct AnchorPath) + 512);
}
else
{
error = TRUE;
}
if (!error)
{
FILE *fd;
if (fd = fopen ((char *) buf, "r"))
{
while (fgets (buf, 256, fd))
{
char *field1, *field2, *field3, *field4, *field5, *field6;
char *ptr;
int index, i;
if (strchr (buf, ';'))
{
{
char *start_ptr, *end_ptr;
if (start_ptr = strdup (buf))
{
strlwr (start_ptr);
if ((end_ptr = strstr (start_ptr, "area:")) || (end_ptr = strstr (start_ptr, "code:")))
index = end_ptr - start_ptr;
else
index = 0;
for (ptr = strtok (buf + index, ":"); ptr; ptr = strtok (NULL, " \t\n"))
{
if (strnicmp (ptr, "AREA", 5) == 0)
{
if (your_area)
free (your_area);
ptr = strtok (NULL, "\n");
while (*ptr == ' ')
ptr++;
your_area = strdup (ptr);
}
if (strnicmp (ptr, "CODE", 6) == 0)
{
if (zip_codes)
free (zip_codes);
ptr = strtok (NULL, "\n");
while (*ptr == ' ')
ptr++;
zip_codes = strdup (ptr);
}
}
free (start_ptr);
}
}
while (ptr = strrchr (buf, ';'))
*ptr = '\000';
}
for (ptr = strtok (buf, " \t\n"), i = 0; ptr && (i <= 5); ptr = strtok (NULL, " \t\n"), i++)
{
switch (i)
{
case 0:
field1 = strdup (ptr);
break;
case 1:
field2 = strdup (ptr);
break;
case 2:
field3 = strdup (ptr);
break;
case 3:
field4 = strdup (ptr);
break;
case 4:
field5 = strdup (ptr);
break;
case 5:
field6 = strdup (ptr);
break;
}
}
if (i == 5 || i == 6)
{
struct record *current_record;
if (current_record = (struct record *) malloc (sizeof (struct record)))
{
if (i == 5)
{
current_record->rec_Field1 = field1;
current_record->rec_Field2 = NULL;
current_record->rec_Field3 = field2;
current_record->rec_Field4 = field3;
current_record->rec_Field5 = field4;
current_record->rec_Field6 = field5;
{
char *ptr;
if (ptr = (char *) malloc (20))
{
sprintf (ptr, "%s %s", current_record->rec_Field1, current_record->rec_Field2);
current_record->rec_Node.ln_Name = ptr;
}
}
AddTail (record_list, (struct Node *) current_record);
}
else if (i == 6)
{
current_record->rec_Field1 = field1;
current_record->rec_Field2 = field2;
current_record->rec_Field3 = field3;
current_record->rec_Field4 = field4;
current_record->rec_Field5 = field5;
current_record->rec_Field6 = field6;
{
char *ptr;
if (ptr = (char *) malloc (20))
{
sprintf (ptr, "%s %s", current_record->rec_Field1, current_record->rec_Field2);
current_record->rec_Node.ln_Name = ptr;
}
}
AddTail (record_list, (struct Node *) current_record);
}
retval = TRUE;
}
}
}
fclose (fd);
}
}
return (retval);
}
VOID
FreeRecordList (struct List * record_list)
{
struct record *current_record;
if (your_area)
free (your_area);
if (zip_codes)
free (zip_codes);
while (current_record = (struct record *) RemHead (record_list))
{
if (current_record->rec_Node.ln_Name)
free (current_record->rec_Node.ln_Name);
if (current_record->rec_Field1)
free (current_record->rec_Field1);
if (current_record->rec_Field2)
free (current_record->rec_Field2);
if (current_record->rec_Field3)
free (current_record->rec_Field3);
if (current_record->rec_Field4)
free (current_record->rec_Field4);
if (current_record->rec_Field5)
free (current_record->rec_Field5);
if (current_record->rec_Field6)
free (current_record->rec_Field6);
free (current_record);
}
}
struct record *
GetRecord (struct List *record_list, int record_num)
{
struct record *which_record;
which_record = (struct record *) record_list->lh_Head;
{
int x;
for (x = 0; x < record_num; x++)
which_record = (struct record *) which_record->rec_Node.ln_Succ;
}
return (which_record);
}
VOID
DisplayRecord (int record_num)
{
{
struct record *selected_record;
if (selected_record = (struct record *) GetRecord (&record_list, record_num))
{
GT_SetGadgetAttrs (Project0Gadgets[7], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Node.ln_Name, TAG_DONE);
GT_SetGadgetAttrs (Project0Gadgets[2], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Field3, TAG_DONE);
GT_SetGadgetAttrs (Project0Gadgets[3], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Field4, TAG_DONE);
GT_SetGadgetAttrs (Project0Gadgets[4], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Field5, TAG_DONE);
GT_SetGadgetAttrs (Project0Gadgets[5], Project0Wnd, NULL, GTTX_Text, selected_record->rec_Field6, TAG_DONE);
}
}
if (your_area)
GT_SetGadgetAttrs (Project0Gadgets[0], Project0Wnd, NULL, GTTX_Text, your_area, TAG_DONE);
if (zip_codes)
GT_SetGadgetAttrs (Project0Gadgets[1], Project0Wnd, NULL, GTTX_Text, zip_codes, TAG_DONE);
}